package com.hong.demo.other.easyrules;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.mvel.MVELRule;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.support.YamlRuleDefinitionReader;
import org.springframework.util.ResourceUtils;

import java.io.FileReader;

public class TestShop {
    public static void main(String[] args) throws Exception {
        Person tom = new Person("Tom", 17);
        Facts facts = new Facts();
        facts.put("person", tom);

        Rule ageRule = new MVELRule()
                .name("age rule")
                .description("Check if person's age is > 18 and marks the person as adult")
                .priority(1)
                .when("person.age > 18")
                .then("person.setAdult(true);");

        Rule alcoholRule = new MVELRuleFactory(new YamlRuleDefinitionReader()).
                createRule(new FileReader(ResourceUtils.getFile("classpath:alcohol-rule.yml")));

        Rules rules = new Rules();
        rules.register(ageRule);
        rules.register(alcoholRule);

        RulesEngine rulesEngine = new DefaultRulesEngine();
        System.out.println("Tom: Hi! can I have some Vodka please?");
        rulesEngine.fire(rules, facts);
    }
}